package mp

import (
	"context"

	"ruikc.com/server/api/internal/db"
	"ruikc.com/server/api/internal/svc"
	"ruikc.com/server/api/internal/types"
	"ruikc.com/server/models"
	xb "xorm.io/builder"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type LessonListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLessonListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LessonListLogic {
	return &LessonListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LessonListLogic) LessonList(req *types.LessonForm) (resp []types.LessonList, err error) {
	var datas []models.Lesson
	conds := xb.And(xb.Eq{"course_id": req.CourseId})
	if req.ChapterId > 0 {
		conds = xb.And(conds, xb.Eq{"chapter_id": req.ChapterId})
	}
	db.FindDatas(&datas, conds, "sort_num ASC,id ASC")
	resp = []types.LessonList{}
	for _, v := range datas {
		tmp := types.LessonList{}
		copier.Copy(&tmp, &v)
		resp = append(resp, tmp)
	}
	return resp, nil
}
